package com.cart.xht.dao;

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.cart.xht.domain.Cart;
import com.ljw.store.entity.Store;
import com.marchant.lyh.domain.Item;


@Transactional
public class CartDaoImp implements CartDao{
	@Resource
	private SessionFactory sessionFactory;

	public boolean insertCart(Cart cart) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().persist(cart);
		return  true;
	}

	public boolean deleteGoodsFromCart(int id,int userid) {
		// TODO Auto-generated method stub
		Query query=sessionFactory.getCurrentSession().createQuery("delete from Cart where itemId=? and userId=?");	
		query.setInteger(0, id);
		query.setInteger(1, userid);
		query.executeUpdate();
		return true;
	}

	public void updateCart(Cart cart) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().update(cart);
	}

	public List<Item> getItem(int id) {
		// TODO Auto-generated method stub
		Query query=sessionFactory.getCurrentSession().createQuery("from Item where itemId=?");	
		query.setInteger(0, id);
		List<Item> item= query.list();
		return item;
	}
	
	public List<Store> getStore(int id) {
		// TODO Auto-generated method stub
		Query query=sessionFactory.getCurrentSession().createQuery("from Store where shopId=?");	
		query.setInteger(0, id);
		List<Store> store= query.list();
		return store;
	}

	public boolean checkCart(int itemId,int userid) {
		// TODO Auto-generated method stub
		Query query=sessionFactory.getCurrentSession().createQuery("from Cart where itemId=? and userId=?");	
		query.setInteger(0, itemId);
		query.setInteger(1, userid);
		List<Item> item= query.list();
		if(item.size()>0){
			return true;
		}
		else
			return false;
	}

	public List<Cart> getCart(int itemId,int userid) {
		// TODO Auto-generated method stub
		Query query=sessionFactory.getCurrentSession().createQuery("from Cart where itemId=? and userId=?");	
		query.setInteger(0, itemId);
		query.setInteger(1, userid);
		List<Cart> cartinfo= query.list();
		return cartinfo;
		
	}
	
	public List<Cart> getCartInfo(int id) {
		// TODO Auto-generated method stub
		Query query=sessionFactory.getCurrentSession().createQuery("from Cart where userId=?");	
		query.setInteger(0, id);
		List<Cart> info= query.list();
		return info;
	}

	public int maxShopId() {
		// TODO Auto-generated method stub
		int maxshopid = 0;
		Query query=sessionFactory.getCurrentSession().createQuery("select Max(shopId) FROM Store");	
		maxshopid = (Integer) query.uniqueResult();
		return maxshopid;
	}
}
